package com.molyfun.parents.activity.course.downloadObserver;

import android.text.TextUtils;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.activity.course.bean.CourseMediaBean;
import com.molyfun.parents.activity.course.view.courseitem.CourseCellFactory;
import com.molyfun.parents.dao.entity.CourseDownloadTask;
import com.molyfun.parents.dao.gen.CourseDownloadTaskDao;
import com.molyfun.parents.dao.gen.DaoMaster;
import com.molyfun.parents.dao.gen.DaoSession;
import com.molyfun.parents.impl.OnCourseDownloadTaskProgressUpdate;
import com.molyfun.parents.rest.bean.CourseDetailResponse;
import com.molyfun.parents.rest.bean.CourseListResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.utils.GsonUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/14/16.
 */
public class CourseDownloadProgressCenter implements OnCourseDownloadTaskProgressUpdate {

    private static CourseDownloadProgressCenter mInstance;
    CourseDownloadTaskDao courseDownloadTaskDao;
    private HashSet<String> courseFinishedDownloadId = new HashSet<>();
    private HashSet<String> courseDownloadingId = new HashSet<>();

    private CourseDownloadProgressCenter(){
        DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(MFPApplication.getInstance(), Const.DB_NAME, null);
        DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        courseDownloadTaskDao = daoSession.getCourseDownloadTaskDao();
        List<CourseDownloadTask> courseDownloadTasks = courseDownloadTaskDao.queryBuilder()
                .build().list();
        if(courseDownloadTasks != null ){
            for(CourseDownloadTask courseDownloadTask : courseDownloadTasks){
                CourseListResponse.CourseBean courseBean = GsonUtil.fromJson(courseDownloadTask.getJsonStr(), CourseListResponse.CourseBean.class);
                courseFinishedDownloadId.add(courseBean.getDkey());
            }
        }
    }

    /**
     * one course download progress only need one observer
     */
    private HashSet<OnCourseDownloadTaskProgressUpdate> mDownloadProgressObservers = new HashSet<>();

    public static CourseDownloadProgressCenter getInstance(){
        if(mInstance == null){
            mInstance = new CourseDownloadProgressCenter();
        }
        return mInstance;
    }

    public void setDownloadCourseStart(String courseDownloadId){
        courseDownloadingId.add(courseDownloadId);
    }

    public CourseDownloadContainer handleCourseDownLoadTask(CourseDetailResponse.CourseDetailBean courseDetailBean){
        if((courseDetailBean.getMediaobjectlist() != null && courseDetailBean.getMediaobjectlist().size() > 0) || (courseDetailBean.getPlanmediaobjectlist() != null && courseDetailBean.getPlanmediaobjectlist().size() > 0)){
            CourseDownloadContainer courseDownloadContainer = new CourseDownloadContainer(courseDetailBean.getDkey());
            courseDownloadContainer.setOnCourseDownloadTaskProgressUpdateListener(CourseDownloadProgressCenter.this);
            if(courseDetailBean.getMediaobjectlist() != null){
                for(CourseMediaBean courseMediaBean : courseDetailBean.getMediaobjectlist()){
                    switch (courseMediaBean.getType()){
                        case CourseCellFactory.CourseType.COURSE_IMAGE:
                            if(!TextUtils.isEmpty(courseMediaBean.getUrl())){
                                courseMediaBean.setUrl(courseMediaBean.getUrl());
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                        case CourseCellFactory.CourseType.COURSE_MUSIC:
                            if(!TextUtils.isEmpty(courseMediaBean.getUrl())){
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                        case CourseCellFactory.CourseType.COURSE_GAME:
                            if(!TextUtils.isEmpty(courseMediaBean.getOfflineurl())){
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                        case CourseCellFactory.CourseType.COURSE_VIDEO:
                            if(!TextUtils.isEmpty(courseMediaBean.getUrl())){
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                    }

                }
            }

            if(courseDetailBean.getPlanmediaobjectlist() != null){
                for(CourseMediaBean courseMediaBean : courseDetailBean.getPlanmediaobjectlist()){
                    switch (courseMediaBean.getType()){
                        case CourseCellFactory.CourseType.COURSE_IMAGE:
                            if(!TextUtils.isEmpty(courseMediaBean.getUrl())){
                                courseMediaBean.setUrl(courseMediaBean.getUrl());
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                        case CourseCellFactory.CourseType.COURSE_MUSIC:
                            if(!TextUtils.isEmpty(courseMediaBean.getUrl())){
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                        case CourseCellFactory.CourseType.COURSE_GAME:
                            if(!TextUtils.isEmpty(courseMediaBean.getOfflineurl())){
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                        case CourseCellFactory.CourseType.COURSE_VIDEO:
                            if(!TextUtils.isEmpty(courseMediaBean.getUrl())){
                                courseDownloadContainer.addCourse(courseMediaBean);
                            }
                            break;
                    }

                }
            }

            if(courseDownloadContainer.getTotalFIleCounts() == 0){
                addFinishedDownloadCourse(courseDetailBean.getDkey());
                onCourseDownloadFinish(courseDetailBean.getDkey());
            }
            return courseDownloadContainer;
        }else{
            addFinishedDownloadCourse(courseDetailBean.getDkey());
            onCourseDownloadFinish(courseDetailBean.getDkey());
        }
        return null;
    }

    @Override
    public void onCourseDownloadStart(String courseId) {
        for(OnCourseDownloadTaskProgressUpdate onCourseDownloadTaskProgressUpdate : mDownloadProgressObservers){
            onCourseDownloadTaskProgressUpdate.onCourseDownloadStart(courseId);
        }
    }

    @Override
    public void onCourseprogressUpdate(String courseId, int percent) {
        List<String> finishedCourseIds = new ArrayList<>();
            for(OnCourseDownloadTaskProgressUpdate onCourseDownloadTaskProgressUpdate : mDownloadProgressObservers){
                onCourseDownloadTaskProgressUpdate.onCourseprogressUpdate(courseId, percent);
                if(percent >= 100){
                    courseDownloadingId.remove(courseId);
                    courseFinishedDownloadId.add(courseId);
                    finishedCourseIds.add(courseId);
                }
            }
            for(String courseIdStr : finishedCourseIds){
                mDownloadProgressObservers.remove(courseIdStr);
            }

    }

    @Override
    public void onCourseDownloadFinish(String courseId) {
        for(OnCourseDownloadTaskProgressUpdate onCourseDownloadTaskProgressUpdate : mDownloadProgressObservers){
            onCourseDownloadTaskProgressUpdate.onCourseDownloadFinish(courseId);
        }
    }

    public boolean isCourseDownloadFinish(String courseId){
        if(courseFinishedDownloadId.contains(courseId)){
            return true;
        }
        return false;
    }

    public boolean isCourseDownloading(String courseId){
        if(courseDownloadingId.contains(courseId)){
            return true;
        }
        return false;
    }

    public void addFinishedDownloadCourse(String courseId){
        if(courseDownloadingId.contains(courseId)){
            courseDownloadingId.remove(courseId);
        }
        if(!courseFinishedDownloadId.contains(courseId)){
            courseFinishedDownloadId.add(courseId);
        }
    }

    /**
     * called when  {@link com.molyfun.parents.activity.common.listactivity.factory.DownLoadFactory} delete course online
     * @param courseId
     */
    public void removeCourseDownloadState(String courseId){
        if(courseFinishedDownloadId.contains(courseId)){
            courseFinishedDownloadId.remove(courseId);
        }
    }

    public void registeObserver( OnCourseDownloadTaskProgressUpdate onCourseDownloadTaskProgressUpdate){
        if(!mDownloadProgressObservers.contains(onCourseDownloadTaskProgressUpdate)){
            mDownloadProgressObservers.add(onCourseDownloadTaskProgressUpdate);
        }
    }

    public boolean isRegisteObserver(OnCourseDownloadTaskProgressUpdate onCourseDownloadTaskProgressUpdate){
        return mDownloadProgressObservers.contains(onCourseDownloadTaskProgressUpdate);
    }

    public void unRegisteObserver(OnCourseDownloadTaskProgressUpdate onCourseDownloadTaskProgressUpdate){
        mDownloadProgressObservers.remove(onCourseDownloadTaskProgressUpdate);
    }

}
